﻿Imports System.Data.OleDb

Partial Public Class GroupProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
        Dim sql As String = "SELECT * FROM groups WHERE ID = @ID"
        conn.Open()
        Dim cmd As New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@ID", Request.QueryString("ID"))
        Dim dbRead As OleDbDataReader = cmd.ExecuteReader()
        While dbRead.Read
            image_GroupImage.ImageUrl = dbRead("GroupImage")
            If image_GroupImage.ImageUrl = "Images/Group/" Then
                image_GroupImage.ImageUrl = "Images/Group/noImageUploaded.png"
            End If
        End While

        'Label1.Text = countMembersInGroup(Request.QueryString("ID")).ToString
        'Dim dbread As OleDbDataReader = 
        'imageButton_ProfileImage.ImageUrl = Request.MapPath("Images/" & userImage)
    End Sub

    Protected Sub linkButton_JoinGroup_Click(ByVal sender As Object, ByVal e As EventArgs) Handles linkButton_JoinGroup.Click
        Dim currentGroupId As Integer = Request.QueryString("ID")
        Dim currentUserId As Integer = getUserID()
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
        Dim sql As String = "INSERT INTO MemberGroupJunction (Group_fkid, UserName_fkid) VALUES (@Group_fkid, @UserName_fkid)"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.CommandType = CommandType.Text
        conn.Open()
        cmd.Parameters.AddWithValue("@Group_fkid", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@UserName_fkid", getUserID())
        cmd.ExecuteNonQuery()
        conn.Close()
        DataList1.DataBind()
    End Sub

    Function getUserID() As Integer
        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
        Dim loginName = User.Identity.Name
        Dim loginId As Integer
        Dim readLoginIdSQL As String = "SELECT UserId FROM aspnet_Users where UserName = @UserName"
        Dim cmd As New OleDbCommand(readLoginIdSQL, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@UserName", loginName)
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        reader.Read()
        loginId = (reader("UserId"))
        conn.Close()
        Return loginId

    End Function

    Function countMembersInGroup(ByVal groupId As Integer)
        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
        Dim memberCountSQL As String = "SELECT MembersByGroupsQuery.GroupName, Count(*) AS MemberCount FROM(MembersByGroupsQuery)GROUP BY MembersByGroupsQuery.GroupName"
        conn.Open()
        Dim memberCountCmd As New OleDbCommand(memberCountSQL, conn)
        Dim memberCountReader = memberCountCmd.ExecuteReader()
        If memberCountReader.HasRows Then
            Dim memberCount As Integer = CInt(memberCountReader("MemberCount"))
            Return memberCount
        End If
        Return 0
    End Function
End Class